<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/27
 * Time: 下午4:19
 */

namespace Home\Controller;
use Think\Controller;

class TaskController extends Controller{

    //单个任务主页
    public function index(){
        $mission_id = $_GET['mission'];
        if(isset($mission_id)) {
            $mission_db = M('Mission');
            $mission_result = $mission_db->getByMission_id($mission_id);
            if($_SESSION['loginUser']['user_id'] == $mission_result['publisher_id']){
                session('entrance', 1);
                $this->assign('curNav', 1);
            }else{
                session('entrance', 0);
                $this->assign('curNav', 0);
            }
            $this->assign('mission_id', $mission_id);
            $this->display();
        } else {
            $this->error('获取任务信息失败');
        }
    }

    //单个任务主页PC
    public function indexPC(){
        $mission_id = $_GET['mission'];
        if(isset($mission_id)) {
            $this->assign('mission_id', $mission_id);
            $this->theme('PC')->display('Mission/mission');
        } else {
            $this->error('获取任务信息失败');
        }
    }

    //单个任务主页PC
    public function missionPC(){
        $mission_id = $_GET['mission'];
        if(isset($mission_id)) {
            $this->assign('mission_id', $mission_id);
            $this->theme('PC')->display('Mission/index');
        } else {
            $this->error('获取任务信息失败');
        }
    }

    //获取单个任务信息
    public function taskInfo(){
        $mission_id = $_GET['mission_id'];
        if(IS_GET){
            $mission_db = D('Mission');
            if(isset($_SESSION['loginUser'])){
                $login = 1;
            }else{
                $login = 2;
            }
            $mission_result = $mission_db->getTaskInfo($mission_id,$login);

            if($mission_result){
                $this->success('获取信息成功',$mission_result);
            }else{
                $this->error('获取信息失败');
            }
        }
    }

    //单个任务主页－一键接活
    public function receiveMission(){
        if(isset($_SESSION['loginUser'])){
            if(IS_POST){
                $data1['mission_id'] = $_POST['mission_id'];
                $data1['follower_id'] = $_SESSION['loginUser']['user_id'];
                $data1['is_selected'] = 1;
                //$message = $_POST['message'];
                //$data1['message'] = $message;
                $miss_user_relate_db = M('Miss_user_relate');
                $miss_user_relate_exist = $miss_user_relate_db->where(array(array('mission_id'=>$data1['mission_id']),array('follower_id'=>$data1['follower_id'])))->select();
                if(is_array($miss_user_relate_exist)){
                    $miss_user_relate_result = $miss_user_relate_db->where(array(array('mission_id'=>$data1['mission_id']),array('follower_id'=>$data1['follower_id'])))->save($data1);
                }else{
                    $miss_user_relate_result = $miss_user_relate_db->add($data1);
                }

                //对发活人公开简历信息
                $mission_db = M('Mission');
                $mission_result = $mission_db->getByMission_id($_POST['mission_id']);
                $ispublic_one = $mission_result['publisher_id'];
                $user_id = $_SESSION['loginUser']['user_id'];
                $resume_db = D('Resume');
                $resume_result = $resume_db->setPublicToOne($ispublic_one,$user_id);

                //存储接活信息
                $data2['publisher_id'] = $_SESSION['loginUser']['user_id'];
                $data2['receiver_id'] = $mission_result['publisher_id'];
                $content = $_POST['message'];
                $url = U('Task/index').'/mission/'.$data1['mission_id'];
                $data2['content'] = $content.'!@#$%^&*'.$mission_result['mission_name'].'!@#$%^&*'.$url;
                $data2['is_read'] = 1;
                ini_set('date.timezone','Asia/Shanghai');
                $data2['time'] = date('Y-m-d H:i:s',time());
                $message_db = M('Message');
                $message_result1 = $message_db->add($data2);

                //发送短信通知
//                $user_db = M("User");
//                $user_result = $user_db->getByUser_id($mission_result['publisher_id']);
//                $phone = $user_result['phone'];
//                $mission_name = $user_result['mission_name'];
//                //$sendContent = '你好，你已通过《'.$mission_name.'》项目的审核 ,请登陆活控查看具体信息！';
//                $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
//                $post_data = "account=cf_gwhz&password=XLyxR3&mobile=".$phone."&content=".rawurlencode("您好，您已通过《".$mission_name."》项目的审核，请及时登录活控（ihuokong.com）查看具体消息。");
//                $result = xml_to_array(post($post_data,$url));

                //系统回复消息已到达
//            $data3['publisher_id'] = $mission_result['publisher_id'];
//            $data3['receiver_id'] = $_SESSION['loginUser']['user_id'];
//            $data3['content'] = "我已收到您的消息，请静候佳音！";
//            $data3['is_read'] = 1;
//            ini_set('date.timezone','Asia/Shanghai');
//            $data3['time'] = date('Y-m-d H:i:s',time());
//            $message_result2 = $message_db->add($data3);

                //dump($miss_user_relate_result);
                if($miss_user_relate_result ){
                    $this->success('接活成功');
                }else{
                    $this->error('接活失败');
                }
            }else{
                $this->error('错位方法');
            }
        }else{
            $this->error('请登录系统');
        }

    }

    //单个任务主页－电话联系
    public function phoneConnect(){
        if(IS_GET){
            $user_id = $_GET['publisher_id'];
            $user_db = D('User');
            $user_result = $user_db->getPhoneInfo($user_id);
            if($user_result){
                $this->success('获取电话成功',$user_result);
            }else{
                $this->error('获取电话失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //单个任务主页－收藏
    public function collectMission(){
        if(IS_GET){
            $data['collecter_id'] = $_SESSION['loginUser']['user_id'];
            $data['collected_id'] = $_GET['mission_id'];
            ini_set('date.timezone','Asia/Shanghai');
            $data['collect_time'] = date('Y-m-d H:i:s',time());
            $data['collect_type'] = 2;
            $collect_db = M('Collect');
            $collect_map['collect_type'] = 2;
            $collect_map['collecter_id'] = $data['collecter_id'];
            $collect_map['collected_id'] = $data['collected_id'];
            $collect_exist = $collect_db->where($collect_map)->select();
            //dump($collect_exist);
            if($collect_exist){
                $collect_result = $collect_db->where($collect_map)->save($data);
            }else{
                $collect_result = $collect_db->add($data);
            }
            if($collect_result){
                $this->success('收藏成功');
            }else{
                $this->error('收藏失败');
            }

        }else{
            $this->error('错误方法');
        }
    }

    //单个任务主页－取消收藏
    public function deleteCollectMission(){
        if(IS_GET){
            $collect_db = M('Collect');
            $collect_map['collect_type'] = 2;
            $collect_map['collecter_id'] = $_SESSION['loginUser']['user_id'];
            $collect_map['collected_id'] = $_GET['mission_id'];
            $collect_result = $collect_db->where($collect_map)->delete();
            if($collect_result){
                $this->success('取消收藏成功');
            }else{
                $this->error('取消收藏失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //单个任务主页－留言
    public function leaveMessage(){
        if(IS_POST){
            $data['publisher_id'] = $_SESSION['loginUser']['user_id'];
            $data['receiver_id'] = $_POST['publisher_id'];
            $data['content'] = $_POST['content'];
            $data['is_read'] = 1;
            ini_set('date.timezone','Asia/Shanghai');
            $data['time'] = date('Y-m-d H:i:s',time());
            $message_db = M('Message');
            $message_result = $message_db->$message_db->add($data);
            if($message_result){
                $this->success('留言成功');
            }else{
                $this->error('留言成功');
            }
        }else{
            $this->error('错误方法');
        }
    }


    //test
    public function test(){
        $mission_id = $_GET['mission_id'];
        $mission_db = D('Mission');
        $mission_result = $mission_db->getTaskInfo($mission_id,1);
        dump($mission_result);
    }

} 